/**
 * @Description:
 * @param roleName 角色名称
 * @param status 角色状态
 * @param  firstdate 开始时间
 * @param  enddate 结束时间
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/11
 */

var roleName = "",
    roleKey = "",
    status = "",
    befdate = "",
    enddate = "";

/**
 * 初始化
 */
$(function () {
    init();
    tableInit();
});

/**
 * 加载layui样式
 */
function init() {
    layui.use(['form', 'laydate'], function () {

        var form = layui.form
            , laydate = layui.laydate
            , nowdate = getNowFormatDate();

        //年月日范围选择选择器
        laydate.render({
            elem: '#month'
            , max: 'nowdate'
            , range: '至'
            , done: function (value) {
                if (value != null && (value.indexOf("至") != -1)) {
                    var date = value.split("至");
                    //  开始日期
                    befdate = date[0].trim();
                    //   结束日期
                    enddate = date[1].trim();
                } else {
                    befdate = "";
                    enddate = "";
                }
            }
        });

        //角色状态选择
        form.on('select(status)', function (data) {
            //  状态值
            if (data.value != null) {
                status = data.value;
            }
        });

    });
}

// 数据展示
function tableInit() {
    layui.use(['form', 'table'], function () {
        var loading = window.top.layer.load(0, {shade: false});
        var form = layui.form
            , table = layui.table;

        //展示已知数据
        table.render({
            elem: '#tb'
            , url: prefixRole + "/list"
            , toolbar: '#toolbar'
            , where: {
                //  设置参数
                "params": JSON.stringify({
                    "roleName": roleName,
                    "roleKey": roleKey,
                    "status": status,
                    "befdate": befdate,
                    "enddate": enddate,
                })
            }
            , cellMinWidth: 200
            , cols:
                [[
                    {checkbox: true, fixed: 'true'}
                    , {type: 'numbers', title: '序号', fixed: 'true'}
                    , {field: 'roleName', title: '角色名称', fixed: 'true'}
                    , {field: 'roleKey', title: '权限字符'}
                    , {field: 'roleSort', title: '显示顺序'}
                    , {
                        field: 'status', title: '帐号状态',
                        templet: function (d) {
                            switch (d.status) {
                                case "0" :
                                    return "<button class=\"layui-btn layui-btn-sm\">正常</button>";
                                case "1" :
                                    return "<button class=\"layui-btn layui-btn-sm layui-btn-warm\">停用</button>";
                                default:
                                    return "<button class=\"layui-btn layui-btn-sm layui-btn-danger\">异常</button>";
                            }

                        }
                    }
                    , {field: 'createTime', title: '创建时间'}
                    , {field: 'remark', title: '备注'}
                    , {fixed: 'right', align: 'center', toolbar: '#tool'}
                ]]
            , page:
                true //是否显示分页
            , limits:
                [10, 20, 30]
            , limit:
                10 //每页默认显示的数量
            , done:
                function () {
                    layTool.toplayer.close(loading);
                }
        });

        //头工具栏事件
        table.on('toolbar(tb)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'insert':
                    insert();
                    break;

                case 'remove':
                    var data = checkStatus.data;
                    remove(data);
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(tb)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
            switch (obj.event) {
                case 'update':
                    update(data);
                    break;
                case 'operate':
                    //打开数据权限页面
                    operate(data);
                    break;
            }
        });

    })
}


/**
 * @Description:    查询事件
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/7
 */

function serach() {
    //  用户名称
    roleName = $("#roleName").val();
    roleKey = $("#roleKey").val();
    tableInit();
}

/**
 * @Description: 添加事件
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/12
 */

function insert() {
    var title = "新增角色信息",
        url = prefixOpen + "/web!system!role!role_insert",
        width = "30%",
        height = "75%";
    //  打开新窗口
    layTool.openDialog(title, url, width, height, insertOk);
}


/**
 *  保存用户点击事件save
 */
function insertOk(index, layero) {
    // 获取子类窗口的form 表单
    var body = layTool.getChildFrame("body", index);
    var form = $(body[0]).find('#form');
    if (validate_form(form[0])) {
        var formObj = form.serializeArray();
        // 发送保存地址
        $.ajax({
            url: prefixRole + "/insert",
            data: formObj,
            type: "POST",
            beforeSend: function () {
                loading = layTool.toplayer.load(0, {shade: false});
            },
            success: function (data) {
                layTool.toplayer.close(loading);
                var errcode = data.errcode;
                var errmsg = data.errmsg;
                if (errcode == "0") {
                    tableInit();
                    layTool.closeLayer(index);
                    layTool.showSucAlert(errmsg);
                } else {
                    layTool.showAlert(errmsg);
                }

            },
            error: function (e) {
                layTool.toplayer.close(loading);
                AjaxErro();
            }
        })
    }
}


/**
 * @Description:    更新操作
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/7
 */

function update(data) {
    var roleId = data.roleId;
    var title = "修改角色信息",
        url = prefixRole + "/openRoleUpdate?roleId=" + roleId,
        width = "30%",
        height = "75%";
    //  打开新窗口
    layTool.openDialog(title, url, width, height, updateOk);
}

/**
 * @Description: 确定更新事件回调
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/7
 */
function updateOk(index, layero) {
    // 获取子类窗口的form 表单
    var body = layTool.getChildFrame("body", index);
    var form = $(body[0]).find('#form');
    if (validate_form(form[0])) {
        var formObj = form.serializeArray();
        // 发送保存地址
        $.ajax({
            url: prefixRole + "/update",
            data: formObj,
            type: "POST",
            beforeSend: function () {
                loading = layTool.toplayer.load(0, {shade: false});
            },
            success: function (data) {
                layTool.toplayer.close(loading);
                var errcode = data.errcode;
                var errmsg = data.errmsg;
                if (errcode == "0") {
                    tableInit();
                    layTool.closeLayer(index);
                    layTool.showSucAlert(errmsg);
                } else {
                    layTool.showAlert(errmsg);
                }

            },
            error: function (e) {
                layTool.toplayer.close(loading);
                AjaxErro();
            }
        })
    }
}

/**
 * @Description:   删除操作
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/8
 */

function remove(data) {
    if (!isEmpty(data)) {
        var dataLength = data.length;

        if (dataLength > 0) {
            layTool.showConfirm("确定要删除您选择的" + dataLength + "个角色吗？", function () {
                var loading = "";
                var roleIds = "";
                for (var i = 0; i < dataLength; i++) {
                    roleIds += data[i].roleId + ",";
                }
                $.ajax({
                    url: prefixRole + "/deleteRole",
                    data: {
                        roleIds: roleIds.substring(0, roleIds.length - 1)
                    },
                    beforeSend: function () {
                        loading = layTool.toplayer.load(0, {shade: false});
                    },
                    success: function (data) {
                        layTool.toplayer.close(loading);
                        if (!isEmpty(data)) {
                            var errcode = data.errcode;
                            var errmsg = data.errmsg;
                            if (errcode == "0") {
                                tableInit();
                                layTool.showSucAlert(errmsg);
                            } else {
                                layTool.showAlert(errmsg);
                            }
                        }
                    },
                    error: function () {
                        layTool.toplayer.close(loading);
                        AjaxErro();
                    }
                })

            });
        } else {
            layTool.showMsg("只能同时选择1个或多个角色，您选择了" + dataLength + "个");
        }
    } else {
        layTool.showMsg("请选择要删除的角色信息");
    }

}

/**
 * @Description: 打开数据权限页面
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/10
 */

function operate(data) {
    var roleId = data.roleId;
    var title = "选择数据权限",
        url = prefixRole + "/openOperate?roleId=" + roleId,
        width = "20%",
        height = "40%";
    //  打开新窗口
    layTool.openDialog(title, url, width, height, operateOk);
}

/**
 * @Description: 设置数据权限
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/14
 */

function operateOk(index, layero) {
    // 获取添加 部门树 页面参数函数
    var body = layTool.getChildFrame("body", index);
    var deptIds = body.find('#deptIds').val();
    var roleId = body.find('#roleId').val();
    // 发送保存地址
    $.ajax({
        url: prefixRole + "/updateOperate",
        data: {
            deptIds: deptIds,
            roleId: roleId,
        },
        type: "POST",
        beforeSend: function () {
            loading = layTool.toplayer.load(0, {shade: false});
        },
        success: function (data) {
            layTool.toplayer.close(loading);
            var errcode = data.errcode;
            var errmsg = data.errmsg;
            if (errcode == "0") {
                tableInit();
                layTool.closeLayer(index);
                layTool.showSucAlert(errmsg);
            } else {
                layTool.showAlert(errmsg);
            }

        },
        error: function (e) {
            layTool.toplayer.close(loading);
            AjaxErro();
        }
    })
}


